package com.enbatis.mybatisplugs.controller;

import com.enbatis.mybatisplugs.base.BaseController;
import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.commons.utils.ResultReturn;
import com.enbatis.mybatisplugs.plugin.Pages;
import com.github.pagehelper.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.enbatis.mybatisplugs.entity.TSales;
import com.enbatis.mybatisplugs.service.TSalesService;


import java.util.List;

@RestController
@RequestMapping("v1/t_sales")
public class TSalesController extends BaseController {

@Autowired
private TSalesService tSalesService;


    @PostMapping("/add")
    public ResultReturn<TSales> add(@RequestBody TSales tSales){
        return  success(tSalesService.insert(tSales));
    }


    @PostMapping("/add_sales")
    public ResultReturn<Boolean> addSales(@RequestParam("productId")Long productId,@RequestParam("num") Integer num,@RequestParam("customerIds")Long [] customerIds){
        return  success(tSalesService.addSales(productId,num,customerIds,getAccount()));
    }


    @DeleteMapping("/delete")
    public ResultReturn<Integer> delete(@RequestParam("id") Long id){
        return success(tSalesService.deleteById(id));
    }


    @PutMapping("/{id}")
    public  ResultReturn<Integer> update(@PathVariable("id") Long id,@RequestBody TSales tSales){
        tSales.setId(id);
        return success(tSalesService.updateById(tSales));
    }


    @GetMapping("/{id}")
    public ResultReturn<TSales> get(@PathVariable("id") Long id) {
        return success(tSalesService.getById(id));
    }


    @GetMapping("/list")
    public ResultReturn<List<TSales>> getList(TSales tSales){
        return success(tSalesService.list(new Wrapper<>(tSales)));
    }


    @ApiOperation(value = "分页列表", notes = "分页查询数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求数据的页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sord", value = "排序方式", paramType = "query", dataType = "String"),
    })
    @PostMapping("/page")
    public ResultReturn<Pages<TSales>> getPage(@RequestBody TSales sales){
        Page<TSales> page= getPage();
        return success(tSalesService.page(page,new Wrapper<>(sales).eq("status",0).eq("uid",getAccount().getId())));
    }



    @ApiOperation(value = "分页列表", notes = "分页查询数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求数据的页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sord", value = "排序方式", paramType = "query", dataType = "String"),
    })
    @PostMapping("/page_his")
    public ResultReturn<Pages<TSales>> getPageHis(@RequestBody TSales sales){
        Page<TSales> page= getPage();
        return success(tSalesService.page(page,new Wrapper<>(sales).ne("status",0).eq("uid",getAccount().getId())));
    }


    @PostMapping("/sales_cancel")
    public ResultReturn<Boolean> salesCancel(@RequestParam("ids") Long[] ids){
        return  success(tSalesService.salesCancel(ids,getAccount()));
    }



    @PostMapping("/sales_end")
    public ResultReturn<Boolean> salesEnd(@RequestParam("ids") Long[] ids){
        return  success(tSalesService.salesEnd(ids,getAccount()));
    }
}
